package com.gcsoftware.web.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.gcsoftware.utils.SysConst;
import com.gcsoftware.utils.log.InvokeLog;
import com.gcsoftware.utils.log.LogUtils;
import com.gcsoftware.web.beans.request.GetGrantRoleReq;
import com.gcsoftware.web.service.T_GW_DSITEM_SERVICE;
import com.gcsoftware.web.service.T_GW_INTERF_SERVICE;
import com.gcsoftware.web.service.T_GW_ROLE_SERVICE;
import com.gcsoftware.web.beans.T_GW_ROLES;
import com.gcsoftware.utils.MapUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by qs on 2018/3/19.
 */
@Controller
@RequestMapping("/roles")
public class RolesController {
    @Autowired
    private T_GW_ROLE_SERVICE service;

    @Autowired
    private T_GW_INTERF_SERVICE interfService;

    @Autowired
    private T_GW_DSITEM_SERVICE dsiService;

    @RequestMapping(value = "")
    public String topage(Model model) {
        model.addAttribute("interfList" , interfService.queryForList(null, SysConst.DICT_INTERF_STATUS_PUBLISH,null,null));
        model.addAttribute("dsiList" , dsiService.queryList(null));
        return "admin/interface-role";
    }

    @RequestMapping(value = "/list" , method = RequestMethod.POST)
    public @ResponseBody List<T_GW_ROLES> queryForList(@RequestParam(value = "c_ROLE_NAME" , required = false) String c_ROLE_NAME) {
        //c_ROLE_TYPE = '0' 角色类型为接口角色 c_ROLE_TYPE = '1' 系统角色
        return service.queryForList("0",c_ROLE_NAME);
    }


    @RequestMapping(value = "/{c_ROLE_ID}/delete" , method = RequestMethod.POST)
    @ResponseBody
    public MapUtils remove(@PathVariable("c_ROLE_ID") String c_ROLE_ID) {
        return  1 == service.delete(c_ROLE_ID) ? MapUtils.ok() : MapUtils.error();
    }


    @RequestMapping(value = "/interfDsi" , method = RequestMethod.POST)
    @ResponseBody
    public Map<String, List<String>> interfDsi(@RequestParam(value = "c_ROLE_ID" ,required = true) String c_ROLE_ID) {
        return service.queryRoleDsiInterfs(c_ROLE_ID);
    }


    @RequestMapping(value = "/save" , method =  RequestMethod.POST)
    @ResponseBody
    @InvokeLog(name = "save Interfs" , description = "新增接口角色" , logType = LogUtils.LOG_TYPE_OPERATE)
    public MapUtils save(T_GW_ROLES role) {
        if (StringUtils.isBlank(role.getC_ROLE_NAME())) {
            return MapUtils.error("角色名称不能为空！");
        }
        if(StringUtils.isNotBlank(role.getC_ROLE_ID())) {
            return service.update(role);
        } else {
            return service.insert(role);
        }
    }
    @RequestMapping(value = "/grant" , method =  RequestMethod.POST)
    @ResponseBody
    public MapUtils grant(@RequestBody String param) {
        GetGrantRoleReq req = JSONObject.parseObject(param , GetGrantRoleReq.class);
        return service.grantRoles(req);
    }
}
